package com.sicent.upms.operate.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sicent.operate.common.base.dao.PageInfo;
import com.sicent.operate.common.base.interf.BaseMapper;
import com.sicent.operate.common.base.service.BaseServiceSupport;
import com.sicent.operate.common.datasource.DataSource;
import com.sicent.upms.operate.dao.ManagerResourceDao;
import com.sicent.upms.operate.dao.ManagerRoleDao;
import com.sicent.upms.operate.dao.ManagerRoleResourceDao;
import com.sicent.upms.operate.dao.ManagerUserInfoDao;
import com.sicent.upms.operate.dao.ManagerUserRoleDao;
import com.sicent.upms.operate.model.ManagerResource;
import com.sicent.upms.operate.model.ManagerRole;
import com.sicent.upms.operate.model.ManagerRoleResource;
import com.sicent.upms.operate.model.ManagerUserInfo;

@Service
@DataSource("baseDataSource")
public class ManagerUserInfoService extends
		BaseServiceSupport<ManagerUserInfo, Integer> {

	@Autowired
	private ManagerUserInfoDao managerUserInfoDao;

	@Autowired
	private ManagerResourceDao managerResourceDao;

	@Autowired
	private ManagerRoleDao managerRoleDao;
	
	@Autowired
	private ManagerRoleResourceDao managerRoleResourceDao;
	
	@Autowired
	private ManagerUserRoleDao managerUserRoleDao;

	@Override
	public BaseMapper<ManagerUserInfo, Integer> getMapper() {
		return managerUserInfoDao;
	}

	public ManagerUserInfo getByUserName(String userName) {
		return managerUserInfoDao.getByUserName(userName);
	}

	public List<ManagerResource> getAllResource(ManagerResource queryInfo) {
		return managerResourceDao.getByPojo(queryInfo);
	}

	public void addResource(ManagerResource record) {
		managerResourceDao.save(record);
	}

	public PageInfo<ManagerRole> getAllRole(PageInfo<ManagerRole> page) {
		Objects.requireNonNull(page);
		List<ManagerRole> rows = managerRoleDao.getByPageList(page);
		page.setRows(rows);
		Integer total = managerRoleDao.getByPageCount(page);
		page.setTotal(total);
		Integer totalPage = total / page.getPageSize();
		if (total % page.getPageSize() != 0) {
			totalPage += 1;
		}
		page.setTotalPage(totalPage);
		return page;
	}

	public void addRole(ManagerRole role) {
		managerRoleDao.save(role);
	}

	public void addRoleResource(Map<Integer, List<Integer>> mapResource,
			Integer roleId) {
		List<ManagerRoleResource> list=new ArrayList<ManagerRoleResource>();
		managerRoleResourceDao.delByRole(roleId);
		//组装对象
		for (Entry<Integer, List<Integer>> entry:mapResource.entrySet()) {
			Integer parentId=entry.getKey();
			ManagerRoleResource  parent=new ManagerRoleResource();
			parent.setRoleId(roleId);
			parent.setResourceId(parentId);
			list.add(parent);
			List<Integer> childs=entry.getValue();
			for (Integer integer : childs) {
				ManagerRoleResource  child=new ManagerRoleResource();
				child.setRoleId(roleId);
				child.setResourceId(integer);
				list.add(child);
			}
		}
		managerRoleResourceDao.saveBatch(list);
	}
	
	public List<ManagerRole> getAllRole(ManagerRole managerRole){
		return managerRoleDao.getByPojo(managerRole);
	}

}
